<template>
	<view>
		<block v-if="apiModal.show">
			<u-modal
				@confirm="onApiConfirm"
				confirm-text="知道了"
				v-model="apiModal.show"
				:show-cancel-button="false"
				:title="apiModal.title"
				:content="apiModal.content"
			></u-modal>
		</block>
	</view>
</template>
<script>
import { mapState, mapMutations } from 'vuex';
import { configList, initHttpConfig } from '@/api/luch-request.js';

export default {
	data() {
		return {
			index: 0,
			apiModal: {
				show: false,
				title: '系统提示',
				content: null
			},
			errModal: {
				show: false,
				title: '系统提示',
				content: null
			}
		};
	},
	onLoad(options) {
		const vm = this;

		// #ifdef MP-ALIPAY
		// 支付宝平台，扫二维码进入小程序，获取二维码参数
		if (getApp().globalData.q) {
			options.q = getApp().globalData.q;
			getApp().globalData.q = null;
		}
		// #endif

		// #ifdef MP-WEIXIN
		wx.hideHomeButton();
		// #endif

		// #ifdef MP-ALIPAY
		my.hideBackHome();
		// #endif

		console.log('onLoad', options);

		// 初始化HTTP配置
		initHttpConfig('page/index/index');

		console.log('options.q', options.q);
		console.log('options.code', options.code);
		console.log('options.apiCode', options.apiCode);

		vm.apiModal.show = false;

		// 尝试获取API接口切换参数
		if (options.apiCode != undefined) {
			console.log('即将切换API接口地址...', options.apiCode);
			vm.switchApi(parseInt(options.apiCode));
			return;
		}

		if (options.q) {
			// 尝试获取API接口切换参数
			let apiCode = vm.$utils.getQuery(options.q, 'apiCode');
			if (apiCode != undefined && apiCode != null) {
				console.log('即将切换API接口地址...', apiCode);
				vm.switchApi(parseInt(apiCode));
				return;
			}

		}
		
		vm.$forceUpdate()

		vm.checkGoPage();
	},
	methods: {
		onApiConfirm() {
			const vm = this;
			uni.reLaunch({
				url: '/pages/index/index'
			});
		},
		switchApi(index) {
			const vm = this;
			vm.$store.commit('app/removeUserInfo');
			if (vm.$XEUtils.indexOf([0, 1, 2, 3], index) != -1) {
				console.clear();
				vm.$utils.clearStorageSync();
				vm.$utils.setStorageSync('api-index', index);
				console.log(`【已切换到${configList[index].name}API接口】${configList[index].baseURL}`);
				vm.apiModal.show = false;
				vm.$nextTick(function() {
					vm.apiModal.title = `已切换到${configList[index].name}API接口地址`;
					vm.apiModal.content = `${configList[index].baseURL}`;
					vm.apiModal.show = true;
				});
			} else {
				vm.apiModal.show = false;
				vm.$nextTick(function() {
					vm.apiModal.title = `无效的参数`;
					vm.apiModal.content = `切换API接口地址失败`;
					vm.apiModal.show = true;
				});
			}
		},
		checkGoPage() {
			const vm = this;
			vm.goWeiLaoHomePage();
		},
		goWeiLaoHomePage() {
			const vm = this;
			uni.reLaunch({
				url: `/pkgMain/pages/index/index`
			});
		}
	}
};
</script>
